<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <link rel="icon" href="images/favicon.ico" />
    <link rel="canonical" href="http://bookshelfjs.org" />
    <title>Bookshelf.js | lib/base/relation.js</title>
    <!--
    ._.                                                      ._.
    | |    ______             _        _          _  __      | |
    | |    | ___ \           | |      | |        | |/ _|     | |
    | |    | |_/ / ___   ___ | | _____| |__   ___| | |_      | |
    | |    | ___ \/ _ \ / _ \| |/ / __| '_ \ / _ \ |  _|     | |
    | |    | |_/ / (_) | (_) |   <\__ \ | | |  __/ | |       | |
    | |____\____/_\___/_\___/|_|\_\___/_|_|_|\___|_|_|_______| |
    |                                                          |
    \________\  /__________________________________\  /________/
              \/                                    \/
    -->
    <script src="scripts/prettify/prettify.js"></script>
    <script src="scripts/prettify/lang-css.js"></script>
    <!--[if lt IE 9]>
      <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->

    <!-- Import fonts from Open Font Library -->
    <link rel="stylesheet" media="screen" href="https://fontlibrary.org/face/carlito" rel="stylesheet" type="text/css"/>
    <link rel="stylesheet" media="screen" href="https://fontlibrary.org/face/average-mono" rel="stylesheet" type="text/css"/>
    <link rel="stylesheet" media="screen" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" type="text/css"/>

    <link type="text/css" rel="stylesheet" href="styles/main.css">
  </head>
  <body class="minami">
    <nav class="main-navbar">
      <a href="index.html"><img class="nav-logo" src="images/bookshelf-icon-inverted.svg" />Bookshelf.js
</a>
      <label for="show-menu" class="show-menu">
        <i class="fa fa-bars fa-lg" aria-hidden="true"></i>
      </label>
      <input type="checkbox" id="show-menu" role="button" checked>
      <ul id="menu">
        
          <li>
            <a href="tutorials.html"><i class="fa fa-file-text" aria-hidden="true"></i>Guides
</a>
          </li>
        
        
          <li>
            <a href="api.html"><i class="fa fa-book" aria-hidden="true"></i>API Reference
</a>
          </li>
        
        <li>
          <a href="https://github.com/bookshelf/bookshelf"><span class="fa fa-github"></span></a>
        </li>
      </ul>
    </nav>

    <div class="page-content">
      

      <div class="main-container source">
        <div class="main-column">
          
            <h1 class="page-title">lib/base/relation.js</h1>
          

          


    
    <section>
        <article>
            <pre class="prettyprint source linenums"><code>const _ = require('lodash');
const CollectionBase = require('./collection');
const extend = require('../extend');

/**
 * @class
 * @param {string} type
 *   The type of relation to create. Can be one of 'hasOne', 'hasMany', 'belongsTo',
 *   'belongsToMany' or 'morphTo'.
 * @param {Model|Collection|null} Target
 *   The target model or collection for this relation or `null` in case the target model will be
 *   determined at a later time, as is the case of `morphTo` relations.
 * @param {object} options
 *   Additional properties to set on the relation object. These vary according to the type of
 *   relation.
 */
function RelationBase(type, Target, options) {
  if (Target) {
    this.targetTableName = _.result(Target.prototype, 'tableName');
    this.targetIdAttribute = _.result(Target.prototype, 'idAttribute');
  }

  Object.assign(this, {type, target: Target}, options);
}

/**
 * Creates a new relation instance. Used by the `Eager` relation when dealing with `morphTo` cases,
 * where the same relation is targeting multiple models. It uses the same argument parameters as the
 * base constructor.
 *
 * @return {RelationBase}
 */
RelationBase.prototype.instance = function instance(type, Target, options) {
  return new this.constructor(type, Target, options);
};

/**
 * Creates a new, unparsed model. Used internally in the eager fetch helper methods because parsing
 * may mutate information necessary for eager pairing.
 *
 * @param {object} data Model attributes to set on the new model.
 * @return {Model} The new model.
 */
RelationBase.prototype.createModel = function createModel(data) {
  if (this.target.prototype instanceof CollectionBase) {
    return new this.target.prototype.model(data)._reset();
  }
  return new this.target(data)._reset();
};

/**
 * Clones a relation. Required by {@link Model#fetchPage}.
 *
 * @todo Can probably be removed for a simpler approach, or just the `instance` method.
 * @return {RelationBase}
 */
RelationBase.prototype.clone = function clone() {
  return new this.constructor(null, null, this);
};

/**
 * Extends the Base Relation.
 *
 * @method
 * @static
 */
RelationBase.extend = extend;

module.exports = RelationBase;
</code></pre>
        </article>
    </section>





        </div>
      </div>
    </div>

    <footer>
      <div>
        Documentation generated by
        <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.3</a>
        
          on
          June 7, 2020
        
      </div>
    </footer>

    <script>prettyPrint();</script>
    <script src="scripts/linenumber.js"></script>
    <script src="scripts/main.js"></script>
  </body>
</html>
